using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Index_Management
{
    public partial class Form5 : Form
    {
        private int _indexCount; 
        private int _fKeyCount;
        private int _triggerCount;
        private string _script;
        private bool _scriptSaved;
        public Form5()
        {
            InitializeComponent();
        }

        private void Form5_Load(object sender, EventArgs e)
        {
            label_FKeyCount.Text = _fKeyCount.ToString();
            label_IndexCount.Text = _indexCount.ToString();
            label_TriggerCount.Text = _triggerCount.ToString();
        }

        public void AddNodeToTree(string element, string parent, string root)
        {
            treeView_Objects.Nodes[root].Nodes[parent].Nodes.Add(element, element);
        }

        public void AddNodeToTree(string element,string root)
        {
            if (root == "")
            {
                treeView_Objects.Nodes.Add(element,element);
            }
            else
            {
                treeView_Objects.Nodes[root].Nodes.Add(element, element);
            }
        }

        public void DisplayCountSummary(int indexCount, int fKeyCount, int triggerCount)
        {
            _indexCount = indexCount;
            _fKeyCount = fKeyCount;
            _triggerCount = triggerCount;
        }

        public void SetScript(string script)
        {
            _script = script;
        }

        private void SaveFile(object sender, EventArgs e)
        {
            DialogResult saveFile = saveFileDialog1.ShowDialog();
            if (saveFile == DialogResult.OK)
            {
                TextWriter _scriptWriter = new StreamWriter(saveFileDialog1.FileName);
                _scriptWriter.WriteLine(_script);
                _scriptWriter.Close();
                _scriptSaved = true;
            }
        }
        
        private void SaveFile()
        {
            DialogResult saveFile = saveFileDialog1.ShowDialog();
            if (saveFile == DialogResult.OK)
            {
                TextWriter _scriptWriter = new StreamWriter(saveFileDialog1.FileName);
                _scriptWriter.WriteLine(_script);
                _scriptWriter.Close();
                _scriptSaved = true;
            }
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            DialogResult cancelMessage = MessageBox.Show("Are you sure you want to exit the wizard?", "Cancel wizard.", MessageBoxButtons.YesNo);
            if (cancelMessage == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void button_Finish_Click(object sender, EventArgs e)
        {
            if (_scriptSaved)
            {
                Application.Exit();
            }
            else
            {
                DialogResult saveFile = MessageBox.Show("Do you want to save the script?", "Save File", MessageBoxButtons.YesNoCancel);
                if (saveFile == DialogResult.Yes)
                {
                    SaveFile();
                }
                if (saveFile == DialogResult.No)
                {
                    Application.Exit();
                }
            }

        }
    }
}